<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateAddressAPIRequest;
use App\Http\Requests\API\UpdateAddressAPIRequest;
use App\Models\Address;
use App\Models\Article;
use App\Models\Goods;
use App\Repositories\AddressRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class AddressController
 * @package App\Http\Controllers\API
 */

class ArticlesAPIController extends ApiBaseController
{
    /** @var  AddressRepository */
    private $addressRepository;
    protected $model_name = 'App\Models\Article';
    protected $model_load_array = [
    ];

    public function __construct(AddressRepository $addressRepo)
    {
        $this->addressRepository = $addressRepo;
    }

    public function authLimit()
    {
        return [
            ['status', '=', 1],
        ];
    }

    public function _after_find(&$data)
    {
        $data->reading_quantity++;
        $data->save();

        $array = $data->toArray();
        if($array['goods']){
            $_goods = Goods::whereIn('id', explode(',', $array['goods']))->get();
            $_goods->load('categories');
            $_goods->load('attributesByStatus');
            $_goods->load('contents');
            $_goods->load('slideshow');

            $_goods_arr = $_goods->toArray();
            foreach ($_goods_arr as &$item) {
                if(isset($item['attributes_by_status']))
                    $item['attributes'] = $item['attributes_by_status'];
                unset($item['attributes_by_status']);
            }
            $array['goods'] = $_goods_arr;
        }
        $data = $array;
    }

    public function point($id)
    {
        $article = Article::find($id);
        if($article){
            $article->point_number++;
            $article->save();
            return responseZK(1, $article);
        }else{
            return responseZK(0,null,'找不到该文章');
        }
    }
}
